/*
 * Copyright the original author or authors.
 * 
 * Licensed under the MOZILLA PUBLIC LICENSE, Version 1.1 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * 
 *	  http://www.mozilla.org/MPL/MPL-1.1.html
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.googlecode.milib.display.ui.slider {
	import com.googlecode.milib.base.IMLBroadcasterListener;
	import com.googlecode.milib.base.MLBroadcastClass;
	import com.googlecode.milib.data.value.MLNumberWithRangeValue;
	import com.googlecode.milib.display.MCDragController;
	import com.googlecode.milib.display.service.MLButton;
	import com.googlecode.milib.display.service.MLMC;
	import com.googlecode.milib.vo.MLEventInfo;

	import flash.display.MovieClip;

	/**
	 * @author Marek Brun
	 */
	public class MCSlider extends MLBroadcastClass implements IMLBroadcasterListener {
		
		public const event_Changed : String = 'Changed';
		
		private var mlmc:MLMC;
		private var mc_bg : MovieClip;
		private var mc_btn : MovieClip;
		private var width :Number=100;
		private var model:MLNumberWithRangeValue;		public var isSelfUpdate:Boolean=true;
		private var btn:MLButton;
		private var dragBtn:MCDragController;
		private var marg:Number;
		
		public function MCSlider(mc:MovieClip) {
			mlmc=MLMC.forInstance(mc);
			mc_bg=MovieClip(mc.getChildByName('bg'));
			mc_btn=MovieClip(mc.getChildByName('btn'));
			marg=mc_btn.x;
			
			btn=mlmc.getMLButton('btn');
			dragBtn=new MCDragController(mc_btn, btn, false);
			dragBtn.addListener(this);
			
			setModel(new MLNumberWithRangeValue(0, 0, 1));
			var orgRotation:Number=mc.rotation;
			mc.rotation=0;
			setWidth(mlmc.mc.width);
			mlmc.mc.scaleX=1;
			mc.rotation=orgRotation;
		}
		
		override public function getPrivateVariables():Array {
			return [
				['model', model]
			].concat(super.getPrivateVariables());
		}
		
		public function setWidth(width:Number):void {
			if(this.width==width){ return; }
			this.width=width;
			mc_bg.width=width;
			draw();
		}
		
		public function draw():void {
			mc_btn.x=marg+model.getPosition()*(width-marg*2);
		}
		
		public function setModel(model:MLNumberWithRangeValue):void {
			if(this.model==model){ return; }
			if(this.model){
				this.model.removeListener(this);
			}
			this.model=model;			this.model.addListener(this);
		}
		
		public function getModel():MLNumberWithRangeValue {
			return model;
		}
		
		public function get mc():MovieClip { return mlmc.mc; }
		
//********************************************************************************************
//		events for MCSlider
//********************************************************************************************
		public function onEvent(ev:MLEventInfo):void {
			switch(ev.hero){
				case model:
					switch(ev.event) {
						case model.event_Changed:
							draw();
							broadcastEvent(event_Changed);
						break;
					}
				break;
				case dragBtn:
					switch(ev.event) {
						case dragBtn.event_NewDragXY:
							model.setPosition((dragBtn.getX()-marg)/(width-marg*2));
						break;
					}
				break;
			}
		}
	}
}
